//
//  AppDelegate.m
//  Bus_iPhone
//
//  Created by antunnel on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "BusLineViewController.h"
#import "BusSetting.h"
#import "SBJson.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
   

    NSArray *classArray=[NSArray arrayWithObjects:@"BusLineViewController",@"BusStopViewController",@"StopToStopViewController",@"SettingViewController", nil];
    NSArray *titleArray=[NSArray arrayWithObjects:@"线路查询",@"站点查询",@"站站查询"/*,@"收藏"*/,@"设置", nil];
    NSArray *imgArray=[NSArray arrayWithObjects:@"line_tab_icon.png",@"stop_tab_icon.png",@"search_tab_icon.png",@"setting_tab_icon.png", nil];
    NSArray *tabTitleArray=[NSArray arrayWithObjects:@"线路查询",@"站点查询",@"转乘查询",@"设置", nil];
    NSMutableArray *tempViewControllers=[NSMutableArray new];
    
    for (int i=0; i<classArray.count; i++) {
        
        UIViewController *controller=[[NSClassFromString([classArray objectAtIndex:i]) alloc]initWithNibName:nil bundle:nil];
        controller.title=[titleArray objectAtIndex:i];
        UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:controller];
        UITabBarItem *item=[[UITabBarItem alloc]initWithTitle:[tabTitleArray objectAtIndex:i] image:[UIImage imageNamed:[imgArray objectAtIndex:i]] tag:i];
        navigationController.tabBarItem=item;
        [item release];
        [tempViewControllers addObject:navigationController];
        [controller release];
        [navigationController release];
    }
    
    UITabBarController *tempTabBarController=[[UITabBarController alloc]initWithNibName:nil bundle:nil];
    tempTabBarController.viewControllers=tempViewControllers;
    self.tabBarController=tempTabBarController;
    [tempViewControllers release];
    [tempTabBarController release];
    //self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.tabBarController;//self.viewController;
    [self.window makeKeyAndVisible];
    
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
    return YES;
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"addDeviceToken",@"action",[NSString stringWithFormat:@"%@",deviceToken],@"token", nil];
    ASIFormDataRequest *request=[[[BusSetting defaultSetting]webService]postRequestWithDic:dic];
    if (request) {
        request.delegate=self;
        request.userInfo=dic;
        [request startAsynchronous];
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

  //  NSLog(@"error %@",error);
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


#pragma mark -ASIHTTPRequest

-(void)requestFinished:(ASIHTTPRequest *)request{

    if ([[request.userInfo objectForKey:@"action"] isEqualToString:@"addDeviceToken"]) {
        NSLog(@"request string %@",request.responseString);
        
        NSDictionary *resultDic=nil;
        if ([self isIos5Later]) {
            resultDic=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
        }else{
            resultDic=[request.responseString JSONValue];
        }
        if (!resultDic) {
            ASIFormDataRequest *tempRequest=[[[BusSetting defaultSetting]webService]postRequestWithDic:request.userInfo];
            tempRequest.userInfo=request.userInfo;
            if (tempRequest) {
                [tempRequest setDelegate:self];
                [tempRequest startAsynchronous];
            }
        }
    }
}

@end
